﻿Imports Microsoft.Xna.Framework

Namespace Global.Runa.UI.Metadata

    Public Class PointConverter
        Inherits TypeConverter

        Public Sub New(g As Game)
            MyBase.New(g)
        End Sub

        Public Overrides Function ConvertFromInvariantString(text As String) As Object
            If text.Contains(",") Then
                Dim fields = text.Split(",")
                For i As Integer = 0 To fields.Length - 1
                    fields(i) = fields(i).Trim()
                Next
                If fields.Length >= 2 Then
                    Select Case fields.Length
                        Case 2
                            Return New Point(Integer.Parse(fields(0)), Integer.Parse(fields(1)))
                        Case Else
                            Throw New ArgumentException("Not valid value for this property.")
                    End Select
                Else
                    Throw New ArgumentException("Not valid value for this property.")
                End If
            Else
                Throw New ArgumentException("Not valid value for this property.")
            End If
        End Function

        Public Overrides Function ConvertFromString(text As String) As Object
            Return ConvertFromInvariantString(text)
        End Function

        Public Overrides Function ConvertToInvariantString(value As Object) As String
            With CType(value, Point)
                Return String.Format("{0}, {1}", .X, .Y)
            End With
        End Function

        Public Overrides Function ConvertToString(value As Object) As String
            Return ConvertToInvariantString(value)
        End Function

        Public Overrides Function CreateInstance(propertyValues As IDictionary) As Object
            Return New Point(Integer.Parse(propertyValues.Values(0).ToString()), Integer.Parse(propertyValues.Values(1).ToString()))
        End Function

        Public Overrides Function GetCreateInstanceSupported() As Boolean
            Return True
        End Function

        Public Overrides Function GetPropertiesSupported() As Boolean
            Return False
        End Function

        Public Overrides Function GetStandardValuesSupported() As Boolean
            Return False
        End Function

    End Class

End Namespace